/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

/**
 * <p>Project: jse2204 - Img1
 * <p>Powered by webrx On 2022-09-26 08:58:34
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Img1 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            gen(300,290,String.format("c:/abc/%s.jpg", UUID.randomUUID().toString().toLowerCase()));
        }
    }

    public static void gen(int ww,int hh,String filename)  {
        int w = ww;
        int h = hh;
        //1 建立图像 TYPE_INT_RGB  TYPE_INT_ARGB TYPE_BYTE_GRAY
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

        //建立画笔
        Graphics2D g = img.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //设置画笔颜色
        g.setColor(Color.WHITE);
        //绘制填充的矩形
        g.fillRect(0,0,w,h);


        //设置画笔颜色
        g.setColor(new Color(255, 0, 0));
        //画一条直线
        g.drawLine(0, 0, w, h);

        Random rand = new Random();
        int len = rand.nextInt(10,100);
        for (int i = 0; i < len; i++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(20)+30));
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);
            int size = rand.nextInt(50) + 50;
            g.fillOval(x, y, size, size);
        }


        //释放画笔占用的内存
        g.dispose();
        //2保存图像 png jpg gif
        try {
            ImageIO.write(img, "jpg", new File(filename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
